package com.lanhai.zhonglai.third;

import com.lanhai.zhonglai.beans.StationDataBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.StationDataTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.utils.Tools;

import java.util.Arrays;
import java.util.List;

public class TuiHuo implements IStationProtocol {
	@Override
	public byte[] agvArrived(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if (type == TransDirectionEnum.OnlyIn) {
			buf[0] = 1;
			buf[2] = 1;
			buf[4] = 0;
		} else {
			buf[0] = 2;
			buf[2] = 0;
			buf[4] = 1;
		}
		return buf;
	}

	@Override
	public byte[] applyLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		if (type == TransDirectionEnum.OnlyIn) {
			buf[0] = 1;
			buf[2] = 2;
			buf[4] = 0;
		} else {
			buf[0] = 2;
			buf[2] = 0;
			buf[4] = 2;
		}
		return buf;
	}

	@Override
	public List<StationDataBean> analyseData(String ip, byte[] buf) {
		StationDataBean obj = new StationDataBean();
		obj.setLine(buf[0]);
		obj.setCmdStr(Tools.byte2HexStr(buf));
		obj.setIp(ip);
		obj.setEventType(StationDataTypeEnum.Error);
		if (buf[2] == 4 || buf[4] == 4) {
			// 机台异常
			obj.setEventType(StationDataTypeEnum.Error);
			return Arrays.asList(obj);
		}
		if (buf[2] == 3 || buf[4] == 3) {
			// 允许离开
			obj.setEventType(StationDataTypeEnum.AllowLeave);
			return Arrays.asList(obj);
		}
		if (buf[2] == 2 || buf[4] == 2) {
			// 允许传输
			obj.setEventType(StationDataTypeEnum.AllowTrans);
			return Arrays.asList(obj);
		}
		if (buf[2] == 5 || buf[4] == 5) {
			// 允许传动
			obj.setEventType(StationDataTypeEnum.Receive);

		} else {

			StationMountBean mb = new StationMountBean();

			obj.setEventType(StationDataTypeEnum.Info);

			// 进料状态,

			int income = buf[6];
			mb.setInNum(income);
			if (income == 0) {
				mb.setAllowIn(true);
			}

			// 出料状态
			int outnum = buf[8];
			mb.setOutNum(outnum);
			if (outnum >= 10) {
				mb.setAllowOut(true);
			}

			obj.setBean(mb);
		}
		return Arrays.asList(obj);
	}

	@Override
	public byte[] agvLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if (type == TransDirectionEnum.OnlyIn) {
			buf[0] = 1;
		} else {
			buf[0] = 2;
		}
		return buf;
	}

	@Override
	public byte[] applyRecieve(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if (type == TransDirectionEnum.OnlyIn) {
			buf[0] = 1;
			buf[2] = 3;
			buf[4] = 0;
		} else {
			buf[0] = 2;
			buf[2] = 0;
			buf[4] = 3;
		}
		return buf;
	}
	@Override
	public byte[] transError(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		if (type == TransDirectionEnum.OnlyIn) {
			buf[0] = 1;
		} else {
			buf[0] = 2;
		}
		return buf;
	}
}
